package com.youlu.campus.service.english;

import com.youlu.campus.entity.english.CompetitionCarousel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

@Slf4j
@Service
public class CompetitionCarouselService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void save(CompetitionCarousel competitionCarousel){
        this.mongoTemplate.save(competitionCarousel);
    }

    public CompetitionCarousel detail(String id){
        return this.mongoTemplate.findById(id,CompetitionCarousel.class);
    }

    public Page<CompetitionCarousel> list(){
        List<CompetitionCarousel> list = findAll();
        if(CollectionUtils.isEmpty(list)){
            list  = new ArrayList<>();
        }
        return new PageImpl<>(list);
    }

    public List<CompetitionCarousel> homeList(Integer type){
        Query query = new Query();
        query.addCriteria(Criteria.where("type").is(type));
        List<CompetitionCarousel> list = this.mongoTemplate.find(query,CompetitionCarousel.class);
        if(CollectionUtils.isNotEmpty(list)){
            return list;
        }
        return list;
    }

    private List<CompetitionCarousel> findAll(){
        return  this.mongoTemplate.findAll(CompetitionCarousel.class);
    }

}
